package com.zkingsoft.actions.admin;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.model.NewAssessment;
import com.zkingsoft.model.TaskExecution;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.NewAssessmentService;
import com.zkingsoft.services.TaskExecutionService;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2016-12-07 11:38
 */
@Controller
@RequestMapping(value = "admin/newAssessment")
public class NewAssessmentController extends BaseController{

	@Resource
	private NewAssessmentService currentService;
	@Resource
	private TaskExecutionService taskExecutionService;
	/**
	 * �б���ʾ
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(NewAssessment newAssessment, PaginationVO pageVo) {
		newAssessment.setMentorId(getMe().getId());
		return showList(currentService, newAssessment, pageVo);
	}
   	
	/**
	 * ���������޸�ҳ��
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(NewAssessment newAssessment) {
		if (newAssessment.getId() != null) {
			return modify(currentService, newAssessment, "newAssessment");
		} else {
			return add(currentService, newAssessment, "newAssessment");
		}
	}
	/**
	 * 选择开发语言
	 */
	@RequestMapping(value="/chooseLanguage")
	public @ResponseBody AjaxResult chooseLanguage(NewAssessment newAssessment){
		newAssessment.setNewId(getMe().getId());
		newAssessment.setCreatTime(new Date());
		int i = currentService.add(newAssessment);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "开发语言选择成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "开发语言选择失败");
		}
	}
	/**
	 * 绑定导师
	 */
	@RequestMapping(value = "/binding")
	public @ResponseBody AjaxResult binding(NewAssessment newAssessment) {
		NewAssessment newAssessment1 = new NewAssessment();
		newAssessment1.setNewId(getMe().getId());
		List<NewAssessment> newAssessmentList = currentService.findByModel(newAssessment1);
		newAssessment.setId(newAssessmentList.get(0).getId());
		newAssessment.setNewId(getMe().getId());
		newAssessment.setCreatTime(new Date());
		//添加之前先查看是否已经绑定导师
		TaskExecution taskExecution = new TaskExecution();
		taskExecution.setNewId(getMe().getId());
		taskExecution.setTaskName("绑定导师");
		List<TaskExecution> taskExecutionList = taskExecutionService.findByModel(taskExecution);
		int i=0;
		//如果已经绑定导师，那么在此绑定导师就是修改导师
		if(taskExecutionList.size()>0){
			i= currentService.modify(newAssessment);
		}else{
			i = currentService.binding(newAssessment);
		}
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "导师绑定成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "导师绑定失败");
		}
	}
	
   	/**
	 * �����޸Ľ���
	 */   
	@SaveRequestToken
   	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		NewAssessment newAssessment;
		if (id != null) {
			newAssessment = currentService.findById(id);
			WebUtil.getRequest().setAttribute("obj", newAssessment);
		}
		return "admin/newAssessment-form";
	}
   	
   	
   	/**
	 * ɾ��
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}
 	
}